package com.consum.controller;

import com.consum.Client.configClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * Created by chenjie on 2017/5/10.
 */
@RestController
public class UserController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private configClient configClient;
    // discoveryClient获取服务列表中，应用名为sc-eureka-first-provider一个服务注册信息
    public String serviceUrl() {
        List<ServiceInstance> list = discoveryClient
                .getInstances("EUREKA-SERVICE");
        if (list != null && list.size() > 0) {
            return String.valueOf(list.get(0).getUri());
        }
        return null;
    }

    @GetMapping("/add/{a}/{b}")
    public Integer findByIdByEurekaServer(@PathVariable Integer a,@PathVariable Integer b) {
        String providerServiceUrl = serviceUrl();
        return this.restTemplate.getForObject(providerServiceUrl + "/add?a=" + a+"&b="+b,
                Integer.class);
    }

    @GetMapping("/config")
    public String findconfig() {
        List<String> services = discoveryClient.getServices();
        String config = configClient.findConfig();
        return config;
    }


}
